package com.qyhl.webtv.module_circle.circle.circleshield;

import android.widget.ImageView;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.qyhl.webtv.module_circle.common.CircleUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/3/7.
 */

public class CircleShieldModel implements CircleShieldContract.CircleShieldModel {
    private CircleShieldPresenter mPresenter;

    public CircleShieldModel(CircleShieldPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    /**
     * 获取屏蔽列表
     */
    @Override
    public void getShieldList() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=defriendList" + "&username=" + userName))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<List<CircleHomeBean.User>>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getListError(1, CircleUtils.getCircleMessage(e.getCode()));
                    }

                    @Override
                    public void onSuccess(List<CircleHomeBean.User> users) {
                        mPresenter.getListSuccess(users);
                    }
                });

    }

    /**
     * 屏蔽用户
     *
     * @param dstUsername
     */
    @Override
    public void doBlock(String dstUsername, final ImageView imageView) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();


        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=block" + "&username=" + userName + "&dstUsername=" + dstUsername
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.blockError("网络异常，屏蔽失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult  = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.blockSuccess("您已屏蔽该用户！", imageView);
                        } else {
                            mPresenter.blockError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });


    }

    /**
     * 解除屏蔽
     *
     * @param dstUsername
     */
    @Override
    public void undoBlock(String dstUsername, final ImageView imageView) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        String userName = CommonUtils.getInstance().getUserName();

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=unblock" + "&username=" +userName + "&dstUsername=" + dstUsername))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.unBlockError("网络异常，取消失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult  = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.unBlockSuccess("解除屏蔽成功！", imageView);
                        } else {
                            mPresenter.unBlockError(CircleUtils.getCircleMessage(apiResult.getCode()));
                        }
                    }
                });

    }
}
